package com.reform.service.impl;

import com.reform.pojo.phase.BatchPhase;
import com.reform.mapper.BatchPhaseMapper;
import com.reform.pojo.phase.PhaseCheck;
import com.reform.res.Result;
import com.reform.res.ResultCode;
import com.reform.service.IBatchPhaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reform.service.IPhaseCheckService;
import com.reform.service.IProjectService;
import com.reform.utils.JSONToArrUtils;
import com.reform.utils.VerifycUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mortale
 * @since 2021-08-06
 */
@Service
public class BatchPhaseServiceImpl extends ServiceImpl<BatchPhaseMapper, BatchPhase> implements IBatchPhaseService {

    @Autowired
    BatchPhaseMapper batchPhaseMapper;
    @Autowired
    IProjectService projectService;
    @Autowired
    IPhaseCheckService phaseCheckService;

    @Override
    public Result deleteBatchPhase(String batchId, String phaseId) {
        Map<String, Object> map = new HashMap<>();
        map.put("phase_id", phaseId);
        map.put("batch_id", batchId);
        if (batchPhaseMapper.deleteByMap(map) > 0)
            return Result.success(ResultCode.SUCCESS);
        return Result.failure(ResultCode.FAILURE);
    }

    /**
     * @param phaseId
     * @param batchIdInfo
     * @param op          处理方式 insert&delete
     * @return
     * @throws SQLException
     */
    private Result handleJSONInfo(String phaseId, String batchIdInfo, String op) throws SQLException {
        //将JSON字符串转换为对应的数组
        List<BatchPhase> batchIdList = JSONToArrUtils.jsonToArr(batchIdInfo, BatchPhase.class);
        if (!VerifycUtils.verifyNullOrEmptyUtils(batchIdList))
            return Result.failure(ResultCode.PARAM_JSON_IS_WRONG);
        assert batchIdList != null;
        int res = 0;
        if ("delete".equals(op)) {
            Map<String, Object> map = new HashMap<>();
            map.put("phase_id", phaseId);
            for (BatchPhase batchPhase : batchIdList) {
                map.put("batch_id", batchPhase.getBatchId());
                batchPhaseMapper.deleteByMap(map);
                phaseCheckService.deletePhaseCheck(phaseId);
            }
            return Result.success(ResultCode.SUCCESS);
        }
        if ("insert".equals(op)) {
            for (BatchPhase batchPhase : batchIdList) {
                List<String> proIdList = projectService.getProIdByBatchId(batchPhase.getBatchId());
                List<PhaseCheck> phaseChecks = new ArrayList<>();
                for (String s : proIdList) {
                    PhaseCheck phaseCheck = new PhaseCheck();
                    phaseCheck.setProId(s);
                    phaseCheck.setPtId(phaseId);
                    phaseChecks.add(phaseCheck);
                }
                phaseCheckService.saveBatch(phaseChecks);
                batchPhase.setPhaseId(phaseId);
                res += batchPhaseMapper.insert(batchPhase);
            }
        }
        if (res == batchIdList.size())
            return Result.success(ResultCode.SUCCESS);
        return Result.failure(ResultCode.FAILURE);
    }


    @Override
    public Result insertBatchPhase(String phaseId, String batchIdInfo) throws SQLException {
        return handleJSONInfo(phaseId, batchIdInfo, "insert");
    }

    @Override
    public Result deleteBatchPhases(String phaseId, String batchIdInfo) throws SQLException {
        return handleJSONInfo(phaseId, batchIdInfo, "delete");
    }

    @Override
    public List<BatchPhase> getBatchPhaseByBatchIdList(List<String> batchIdList) {
        String batchIds = "";
        if (batchIdList.size() > 0) {
            batchIds += "'" + batchIdList.get(0) + "'";
            for (int i = 1; i < batchIdList.size(); i++) {
                batchIds += ",'" + batchIdList.get(i) + "'";
            }
        }
        return batchPhaseMapper.getBatchPhaseByBatchIdList(batchIds);
    }
}
